{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Assembly Language\n", "\n", "## Fibonacci \n", "\n", "For the Little Computer 3\n", "\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============================================================\n", "Memory disassembled:\n", "============================================================\n", " x3000: x260D LD R3, N_VAL [line: 1]\n", "LOOP: x3001: x0407 BRz DONE (or 7) [line: 2]\n", " x3002: x2009 LD R0, I_VAL [line: 3]\n", " x3003: x2209 LD R1, J_VAL [line: 4]\n", " x3004: x1401 ADD R2, R0, R1 [line: 6]\n", " x3005: x3206 ST R1, I_VAL [line: 8]\n", " x3006: x3406 ST R2, J_VAL [line: 9]\n", " x3007: x16FF ADD R3, R3, #-1 [line: 11]\n", " x3008: x0FF8 BRnzp LOOP [line: 12]\n", "DONE: x3009: x3401 ST R2, RESULT [line: 14]\n", " x300A: xF025 HALT [line: 16]\n", "RESULT: x300B: x0000 NOOP - (no BR to I_VAL) (or 0) [line: 17]\n", "I_VAL: x300C: x0001 NOOP - (no BR to N_VAL) (or 1) [line: 18]\n", "J_VAL: x300D: x0000 NOOP - (no BR to N_VAL) (or 0) [line: 19]\n", "N_VAL: x300E: x0014 NOOP - (no BR to x3023) (or 20) [line: 20]\n", "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x300F\n", "N: 0 Z: 1 P: 0 \n", "R0: x0000 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x0000 \n" ] } ], "source": [ " .ORIG x3000 \n", " LD R3, N_VAL\n", "Loop BRz Done ; check if last operation flagged z-bit\n", " LD R0, I_VAL ; load i\n", " LD R1, J_VAL ; load j\n", "\n", " ADD R2, R0, R1 ; t = i + j\n", "\n", " ST R1, I_VAL ; j = i (R1)\n", " ST R2, J_VAL ; j = t (R2)\n", "\n", " ADD R3, R3, -1 ; count down n\n", " BR Loop\n", "\n", "Done ST R2, RESULT ; Result in R2\n", " \n", " HALT\n", "RESULT .FILL x0000 \n", "I_VAL .FILL x0001\n", "J_VAL .FILL x0000\n", "N_VAL .FILL 20 ; n -> fib(n) fib(20) = #6765 = x1A6D\n", " .END \n", " " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============================================================\n", "Computation completed\n", "============================================================\n", "Instructions: 164\n", "Cycles: 1274 (0.000637 milliseconds)\n", "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x048E\n", "N: 0 Z: 1 P: 0 \n", "R0: x0A18 R1: x1055 R2: x1A6D R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x300B \n" ] } ], "source": [ "%exe" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6765\n", "6765\n" ] } ], "source": [ "%%python\n", "\n", "print(0x1A6D)\n", "\n", "def fib(n):\n", " if n < 3:\n", " return 1\n", " else:\n", " return fib(n - 1) + fib(n - 2)\n", " \n", "print(fib(20))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Calysto LC3", "language": "gas", "name": "calysto_lc3" }, "language_info": { "codemirror_mode": { "name": "gas", "version": 3 }, "file_extension": ".asm", "mimetype": "text/x-gas", "name": "gas" } }, "nbformat": 4, "nbformat_minor": 0 }